package de.lulusoft.anothertorrent.gui.wizards;

import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

import de.lulusoft.anothertorrent.core.AnotherTorrentCore;
import de.lulusoft.anothertorrent.core.servers.Server;
import de.lulusoft.anothertorrent.core.servers.ServerType;

public class NewServerWizardPage extends WizardPage {
	
	Text url = null;
	Text name = null;
	Combo type = null;

	protected NewServerWizardPage(String pageName) {
		super(pageName);
		setTitle(Messages.NewServerWizardPage_0);
		setDescription(Messages.NewServerWizardPage_1);
	}

	@Override
	public void createControl(Composite parent) {
		parent.setLayout(new FillLayout());
		Composite main = new Composite(parent, SWT.NONE);
		main.setLayout(new GridLayout(2, false));
		GridData gd = new GridData();
		gd.grabExcessVerticalSpace = true;
		gd.verticalAlignment = SWT.FILL;
		gd.horizontalAlignment = SWT.FILL;
		gd.grabExcessHorizontalSpace = true;
		GridData gdInput = new GridData();
		gdInput.grabExcessVerticalSpace = true;
		gdInput.verticalAlignment = SWT.FILL;
		gdInput.horizontalAlignment = SWT.FILL;
		gdInput.grabExcessHorizontalSpace = true;
		
		Label lurl = new Label(main, SWT.NONE);
		lurl.setLayoutData(gd);
		lurl.setText(Messages.NewServerWizardPage_2);
		url = new Text(main, SWT.BORDER | SWT.SINGLE);
		url.setText("http://192.168.88.1:37002/RPC2");
		url.setLayoutData(gdInput);
		url.addModifyListener(new ModifyListener() {
		
			@Override
			public void modifyText(ModifyEvent e) {
				setPageComplete(isPageComplete());
			}
		});
		
		Label lname = new Label(main, SWT.NONE);
		lname.setLayoutData(gd);
		lname.setText(Messages.NewServerWizardPage_4);
		name = new Text(main, SWT.BORDER | SWT.SINGLE);
		name.setLayoutData(gdInput);
		name.addModifyListener(new ModifyListener() {
		
			@Override
			public void modifyText(ModifyEvent e) {
				setPageComplete(isPageComplete());
			}
		});
		
		Label ltype = new Label(main, SWT.NONE);
		ltype.setLayoutData(gd);
		ltype.setText(Messages.NewServerWizardPage_5);
		type = new Combo(main, SWT.BORDER);
		type.setLayoutData(gdInput);
		type.add(ServerType.XMLRPC.name());
		type.select(0);
		type.addModifyListener(new ModifyListener() {
		
			@Override
			public void modifyText(ModifyEvent e) {
				setPageComplete(isPageComplete());
			}
		});
		
		main.pack();
		setControl(main);
	}
	
	@Override
	public boolean isPageComplete() {
		// TODO: Do real checks here..
		if (url != null && url.getText().trim().length() > 0 // make sure url is set
				&& name != null && name.getText().trim().length() > 0 // make sure name is set
				&& !AnotherTorrentCore.getDefault().getServerManager().serverExists(name.getText().trim())
				&& type != null && type.getSelectionIndex() != -1)
			return true;
		return false;
	}

	public Server getServer()
	{
		Server server = new Server(url.getText().trim(),
				ServerType.valueOf(type.getItem(type.getSelectionIndex())),
				name.getText().trim());
		
		return server;
	}

}
